#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
namespace zxws
{
	class string
	{
	public:
		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(strlen(str))
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		void swap(string& tmp)
		{
			std::swap(_str, tmp._str);
			std::swap(_size, tmp._size);
			std::swap(_capacity, tmp._capacity);
		}

		string(const string& s)
			:_str(nullptr)
			,_size(0)
			,_capacity(0)
		{
			string tmp(s._str);
			swap(tmp);
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		const char* c_str() const
		{
			return _str;
		}
		string& operator=(string tmp)
		{
			swap(tmp);
			return *this;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity()const
		{
			return _capacity;
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}

		void resize(size_t n,char x = '\0')
		{
			if (n <= _size)
			{
				_str[n] = '\0';
				_size = n;
			}
			else
			{
				reserve(n);
				while (n--)
				{
					push_back(x);
				}
			}
		}

		void push_back(char x)
		{
			if (_size == _capacity)
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			_str[_size++] = x;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
				reserve(_size + len);
			strcpy(_str+_size, str);
			_size += len;
		}

		void append(size_t n, char x)
		{
			reserve(_size + n);
			while (n--)
			{
				push_back(x);
			}
		}

		string& operator+=(char x)
		{
			push_back(x);
			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		size_t find(char x, size_t pos = 0)
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; i++)
			{
				if (x == _str[pos])
					return i;
			}
			return npos;
		}

		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos < _size);
			char* p = strstr(_str, str);
			return p == nullptr ? npos : p - _str;
		}

		void insert(size_t pos, char x)
		{
			assert(pos <= _size);
			reserve(_size + 1);
			for (size_t i = _size + 1; i > pos; i--)
			{
				_str[i] = _str[i - 1];
			}
			_str[pos] = x;
			_size++;
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos < _size);
			size_t len = strlen(str);
			reserve(_size + len);
			for (size_t i =_size+1,j=_size+len ; i > pos; i--,j--)
			{
				_str[j] = _str[i - 1];
			}
			strncpy(_str + pos, str, len);
			_size += len;
		}

		void erase(size_t pos,size_t n=npos)
		{
			assert(pos < _size);
			if (n == npos || pos + n >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				for (size_t i = pos, j = pos + n; j <= _size; i++, j++)
				{
					_str[i] = _str[j];
				}
				_size -= n;
			}
		}

		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}
		
		const_iterator begin() const
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		string substr(size_t pos,size_t n=npos) const
		{
			assert(pos < _size);
			string tmp;
			size_t end = pos + n;
			if (n == npos || pos + n >= _size)
			{
				n = _size - pos;
				end = _size;
			}
			tmp.reserve(n);
			for (size_t i = pos; i < end; i++)
				tmp += _str[i];
			return tmp;
		}

		bool operator<(const string& s) const
		{
			return strcmp(_str, s._str) < 0;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator<=(const string& s) const
		{
			return *this<s||*this==s;
		}

		bool operator>(const string& s) const
		{
			return !(*this <= s);
		}

		bool operator>=(const string& s) const
		{
			return !(*this < s);
		}

		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		const static size_t npos;

		friend ostream& operator<<(ostream& cout, const string& s);
		friend istream& operator>>(istream& cin, const string& s);
	};
	const size_t string::npos = -1;

	ostream& operator<<(ostream& cout, const string& s)
	{
		for (size_t i = 0; i < s._size; i++)
		{
			cout << s[i];
		}
		return cout;
	}

	istream& operator>>(istream& cin, string& s)
	{
		s.clear();
		char buff[129] = { 0 };
		char x = getchar();
		int i = 0;
		while (x == ' ' || x == '\n')
			x = getchar();
		while (x != '\n' && x != ' ')
		{
			buff[i++] = x;
			if (i == 128)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			x = getchar();
		}
		if (i) {
			buff[i] = '\0';
			s += buff;
		}
		return cin;
	}
};